<script lang="ts" setup>
import { reactive, ref } from "vue"
import { useRouter } from "vue-router"
import { useUserStore } from "@/store/modules/user"
import { User, Lock } from "@element-plus/icons-vue"
import ThemeSwitch from "@/components/ThemeSwitch/index.vue"
import { type FormInstance, FormRules } from "element-plus"
import { type ILoginRequestData } from "@/api/login/types/login"
import { getCodeApi } from "@/api/admin"
import { useFullscreenLoading } from "@/hooks/useFullscreenLoading"
// import loginBg from "/images/login-bg.png"

const router = useRouter()

const codeImage = ref("")
const loginFormRef = ref<FormInstance | null>(null)
const userType = ref<"admin" | "student">("admin")
/** 登录按钮 Loading */
const loading = ref(false)
const fullLoading = ref(false)
/** 登录表单数据 */
const loginForm: ILoginRequestData = reactive({
  username: "",
  password: "",
  code: "",
  remenber: true
})
/** 登录表单校验规则 */

const loginFormRules = ref<FormRules>({
  username: [{ required: true, message: "请输入", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
  code: [{ required: true, message: "请输入验证码", trigger: "blur" }]
})
const getCode = () => {
  fullLoading.value = true
  getCodeApi()
    .then((res) => {
      const url = window.URL.createObjectURL(new Blob([res.blob], { type: "image/png" }))
      codeImage.value = url
      console.log(url)
    })
    .finally(() => {
      fullLoading.value = false
    })
}

getCode()
const loginLoding = useFullscreenLoading(
  () => {
    useUserStore()
      .login(loginForm)
      .then(() => {
        router.push({ path: "/" })
      })
      .catch(() => {
        loginForm.password = ""
      })
      .finally(() => {
        loading.value = false
      })
  },
  {
    text: "登录中"
  }
)
/** 登录逻辑 */
const handleLogin = () => {
  loginFormRef.value?.validate((valid: boolean) => {
    if (valid) {
      if (userType.value === "admin") {
        loginLoding()
      } else {
        useUserStore()
          .login_student(loginForm)
          .then(() => {
            router.push({ path: "/" })
          })
          .catch(() => {
            loginForm.password = ""
            loginForm.code = ""
          })
          .finally(() => {
            loading.value = false
          })
      }
    } else {
      return false
    }
  })
}
</script>

<template>
  <div class="login-container" v-loading="fullLoading">
    <ThemeSwitch class="theme-switch" />
    <div class="login-card">
      <div class="title" style="-webkit-user-select: none; -moz-user-select: none">
        <img src="@/assets/layout/logo-text-2.png" />
        <span>非学历教育管理系统</span>
      </div>
      <div class="content">
        <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" @keyup.enter="handleLogin">
          <el-form-item prop="username">
            <el-input
              v-model.trim="loginForm.username"
              :placeholder="userType === 'admin' ? '工号' : '身份证号'"
              type="text"
              tabindex="1"
              :prefix-icon="User"
              size="large"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model.trim="loginForm.password"
              placeholder="密码"
              type="password"
              tabindex="2"
              :prefix-icon="Lock"
              size="large"
              show-password
            />
          </el-form-item>
          <el-form-item prop="code">
            <el-row :gutter="20">
              <el-col :span="11">
                <el-input
                  v-model.trim="loginForm.code"
                  placeholder="验证码"
                  tabindex="3"
                  :prefix-icon="Lock"
                  size="large"
              /></el-col>
              <el-col :span="6">
                <div class="h-full flex justify-center items-center">
                  <el-image @click="getCode" class="h-full w-full" :src="codeImage" />
                </div>
              </el-col>
              <el-col :span="7">
                <el-button @click="getCode" style="height: 100%" type="primary">获取验证码</el-button></el-col
              >
            </el-row>
          </el-form-item>
          <el-form-item>
            <div class="flex justify-between items-center w-full">
              <div>
                <el-radio-group v-model="userType">
                  <el-radio label="admin">管理员</el-radio>
                  <el-radio label="student">学员</el-radio>
                </el-radio-group>
              </div>
              <el-checkbox v-model="loginForm.remenber" label="记住我" size="large" />
            </div>
          </el-form-item>

          <el-form-item />
          <el-button :loading="loading" type="primary" size="large" @click.prevent="handleLogin"> 登 录 </el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  min-height: 100%;
  background-image: url("/public/images/login-bg.jpg");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  .theme-switch {
    position: fixed;
    top: 5%;
    right: 5%;
    cursor: pointer;
  }
  .login-card {
    width: 480px;
    border-radius: 20px;
    box-shadow: 0 0 10px #dcdfe6;
    background-color: rgba($color: #fff, $alpha: 0.85);
    overflow: hidden;
    .title {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      height: 150px;
      font-family: "Alimama";
      font-size: 28px;
      color: #303133;
      img {
        height: 100%;
      }
    }
    .content {
      padding: 20px 50px 50px 50px;
      :deep(.el-input-group__append) {
        padding: 0;
        overflow: hidden;
        .el-image {
          width: 100px;
          height: 40px;
          border-left: 0px;
          user-select: none;
          cursor: pointer;
          text-align: center;
        }
      }
      .el-button {
        width: 100%;
      }
    }
  }
}
</style>
